package com.example.myapplicationndk.mvp.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.example.myapplicationndk.mvp.base.BasePresenter;
import com.example.myapplicationndk.mvp.base.BaseView;
import com.example.myapplicationndk.mvp.bean.User;
import com.example.myapplicationndk.mvp.model.MainModel;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class MainPresenter implements BasePresenter {

    private BaseView mMainView;
    private MainModel mModel;

    public MainPresenter() {
        mModel = new MainModel();
    }

    @Override
    public void attachView(BaseView view) {
        mMainView = view;
    }

    @Override
    public void detachView() {
        mMainView = null;
    }

    @Override
    public void searchUser(String loginName) {
        if (TextUtils.isEmpty(loginName.trim())) {
            mMainView.showErrorMessage("请输入合法登录名");
            return;
        }
        if (mModel != null) {
            mModel.getUser(new Observer<User>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(User user) {
                    if (user == null) {
                        user = new User("flybigpig", "sda", 1, 3);
                    }
                    Log.d(this.getClass().getName(), user.toString());
                    mMainView.hideProgressDialog();
                    mMainView.showText(user);
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }

            }, loginName);
        }

    }
}